package com.hl.godsent.utils;

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.pm.ActivityInfo
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.webkit.JavascriptInterface
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import com.blankj.utilcode.util.AppUtils
import com.google.gson.JsonParser
import com.hl.godsent.BuildConfig
import com.hl.godsent.GodSent
import com.hl.godsent.Interface.IDownloadListener
import com.hl.godsent.R
import com.hl.godsent.constant.JavaScriptEnum
import com.hl.godsent.constant.ScreenOrientation
import java.io.File


class JavaScriptObject(var mActivity: Activity) {

    private var updateDialog: AlertDialog? = null

    // 设置方向
    @JavascriptInterface
    fun setScreenOrientation(paramer: String?, returnFun: String?) {
        val jsonElement = JsonParser.parseString(paramer)
        val jsonObject = jsonElement.asJsonObject
        if (jsonObject["instruct"].asString == JavaScriptEnum.CHANGE_SCREEN_ORIENTATION.code) {
            if (jsonObject["operate"].asString == ScreenOrientation.VERTICAL) {
                mActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            } else {
                mActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            }
        } else if (jsonObject["instruct"].asString == JavaScriptEnum.NEED_UPDATED.code) {
            val json = jsonObject["operate"].asJsonObject
            Log.i("TAF", "${BuildConfig.VERSION_CODE} < ${json["version"].asInt}")
            if (BuildConfig.VERSION_CODE < json["version"].asInt) {
                this.showUpdateDialog(json["desc"].asString, json["url"].asString)
            }
        }
    }
    @SuppressLint("SetTextI18n")
    private fun showUpdateDialog (desc: String, url: String) {

        val updateView = LayoutInflater.from(mActivity).inflate(R.layout.dialog_update, null, false)
        updateDialog = AlertDialog.Builder(mActivity).setView(updateView).create()
        updateView.findViewById<TextView>(R.id.update_version_info).text = desc
        val mProgressBar: ProgressBar = updateView.findViewById(R.id.id_progress)
        val updateProgress:LinearLayout = updateView.findViewById(R.id.update_progress)
//        点击空白处不消失
        updateDialog?.setCanceledOnTouchOutside(false)
        updateDialog?.setCancelable(false)
        val updateBtn = updateView.findViewById<Button>(R.id.dialog_update_button)

        val progressTest = updateView.findViewById<TextView>(R.id.progress_test)
        updateBtn.setOnClickListener {
            updateProgress.visibility = View.VISIBLE
            updateBtn.visibility = View.GONE
            DownloadUtil().download(GodSent.getGodSentContext(), url, object: IDownloadListener{
                override fun onDownloadSuccess(file: File) {
                    Log.i("TAG", "file: $file")
                    AppUtils.installApp(file)
                    updateDialog?.dismiss()
                }

                override fun onDownloading(progress: Int) {
                    Log.i("TAG", "进度: $progress")
                    mProgressBar.progress = progress
                    progressTest.text = "正在更新 ${progress}%"
                }

                override fun onDownloadFailed() {
                    Log.i("TAG", "失败")
                    updateProgress.visibility = View.GONE
                    updateBtn.visibility = View.VISIBLE
                    updateBtn.text = "更新失败，点击重试"
                }
            })
        }
        updateDialog?.setCanceledOnTouchOutside(false)
        updateDialog?.window?.setBackgroundDrawableResource(R.drawable.transparent)
        updateDialog?.show()
        updateDialog?.window?.setLayout((ScreenSizeUtils.getScreenWidth(mActivity)* 0.36).toInt(), LinearLayout.LayoutParams.WRAP_CONTENT)
    }
}